# -*- coding: utf-8 -*-
# @FileName : yuekao.py
# @Author   : zme
# @Time     : 2025-09-04 16:03
# 1. 导入相关的工具包
import numpy as np
from sklearn import datasets
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score

# 2. 导入葡萄酒数据，并且获取到相应的X和Y
wine = datasets.load_wine()
X = wine.data  # 特征数据
y = wine.target  # 目标变量

# 3. 使用相应的方法，将数据进行标准化
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# 4. 导入相关的数据切分包，将X,Y切分，比例为7:3
X_train, X_test, y_train, y_test = train_test_split(
    X_scaled, y, test_size=0.3, random_state=42  # random_state确保结果可重现
)

# 5. 调用SVM相应模型，设置其中的参数：C=10，gamma=0.1
# 使用rbf核函数实现非线性分类
svm_model = SVC(C=10, gamma=0.1, kernel='rbf', random_state=42)

# 6. 调用训练的方法fit
svm_model.fit(X_train, y_train)

# 7. 计算模型在测试集上的准确率
y_pred = svm_model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f"模型在测试集上的准确率: {accuracy:.4f}")

# 8. 通过训练好的模型预测测试集的结果
test_predictions = svm_model.predict(X_test)
print("测试集的预测结果:")
print(test_predictions)
